package sum.book.common.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
public class CommonMetaObjectHandler implements MetaObjectHandler {

    // 暂时无登录 不需要动态插入 用户Id
    // UserUtil userUtil;

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "gmtCreate", Date.class, new Date());
        this.strictInsertFill(metaObject, "gmtModified", Date.class, new Date());
        this.strictInsertFill(metaObject, "isDeleted", Boolean.class, false);
        this.strictInsertFill(metaObject, "version", Long.class, 1L);

        // String createBy = userUtil.getUserId();
        String createBy = "默认用户";

        if (StringUtils.isNotBlank(createBy)) {
            this.strictInsertFill(metaObject, "createBy", String.class, createBy);
            this.strictInsertFill(metaObject, "modifiedBy", String.class, createBy);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //因为不置为空，当原updateDate有值时不会覆盖，故而此处先置空
        metaObject.setValue("gmtModified", null);
        this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date());

        // String modifiedBy = userUtil.getUserId();
        String modifiedBy = "默认用户";

        if (StringUtils.isNotBlank(modifiedBy)) {
            metaObject.setValue("modifiedBy", null);
            this.strictInsertFill(metaObject, "modifiedBy", String.class, modifiedBy);
        }
    }


}
